package com.plugin;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;

import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RenameJavaMapperPlugin extends PluginAdapter {
    private final Properties properties;

    private String searchString;
    private String replaceString;
    private Pattern pattern;


    public RenameJavaMapperPlugin() {
        this.properties = new Properties();
    }

    public RenameJavaMapperPlugin(Properties properties) {
        this.properties = properties;
    }

    @Override
    public void setProperties(Properties properties) {
        this.properties.putAll(properties);
        searchString = properties.getProperty("searchString"); //$NON-NLS-1$
        replaceString = properties.getProperty("replaceString"); //$NON-NLS-1$
        pattern = Pattern.compile(searchString);
    }

    @Override
    public boolean validate(List<String> warnings) {

        return true;
    }

    @Override
    public void initialized(IntrospectedTable introspectedTable) {
        pattern = Pattern.compile(searchString);
        String oldType = introspectedTable.getMyBatis3JavaMapperType();
        Matcher matcher = pattern.matcher(oldType);
        oldType = matcher.replaceAll(replaceString);

        introspectedTable.setMyBatis3JavaMapperType(oldType);
    }
}